#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
    char a;
    int b;
    struct list *pNext;
} Node;

//内存分配失败
int create_Erro(Node *node)
{
    if (node==NULL)
    {
        printf("节点创建失败");
        return -1;
    }
    return 1;  
}

Node* create_head()
{
    Node *head = (Node *)malloc(sizeof(Node));
    if (create_Erro(head)==-1) return NULL;
    head->a = '!';
    head->b = -999;
    head->pNext = NULL;
    return head;
}

Node* find_PreNode(Node *head, char a, int b)
{
    Node *p = head;
    while (p->pNext != NULL) {
        if (p->pNext->a == a && p->pNext->b == b) {
            return p;
        }
        p = p->pNext;
    }
    return NULL;
}

void PreAdd_Node(Node* head, char a, int b) 
{
    Node *newNode = (Node *)malloc(sizeof(Node));
    if (create_Erro(head)==-1) return;
    newNode->a = a;
    newNode->b = b;
    newNode->pNext = head->pNext;
    head->pNext = newNode;
}

void MidAdd_Node(Node *head, char olda, int oldb, char newa, int newb) 
{
    Node *newNode = (Node *)malloc(sizeof(Node));
    if (create_Erro(head)==-1) return;
    newNode->a = newa;
    newNode->b = newb;
    Node *pre = find_PreNode(head, olda, oldb);
    if (pre != NULL && pre->pNext != NULL)
    {
        // newNode->pNext = pre->pNext;//查找的前面插入
        // pre->pNext = newNode;
        newNode->pNext = pre->pNext->pNext;//查找的后面插入
        pre->pNext->pNext = newNode;
    }
}

void TailAdd_Node(Node* head, char a, int b) 
{
    Node *pre = head;
    while (pre->pNext != NULL)
     {
        pre = pre->pNext;
    }
    Node *newNode = (Node *)malloc(sizeof(Node));
    if (create_Erro(head)==-1) return;
    newNode->a = a;
    newNode->b = b;
    newNode->pNext = NULL;
    pre->pNext = newNode;
}

void print_list(Node *head) 
{
    Node *p = head->pNext;
    while (p != NULL) {
        printf("%c ", p->a);
        printf("%d ", p->b);
        p = p->pNext;
    }
    printf("\n");
}

void del_node(Node *head, char a, int b) 
{
    Node* preNode = find_PreNode(head, a, b);
    if (preNode != NULL && preNode->pNext != NULL)
     {
        Node* delNode = preNode->pNext;
        preNode->pNext = delNode->pNext;
        free(delNode);
    }
}

void modValue_Node(Node *head, char olda, int oldb, char newa, int newb)
{
    Node *pre = find_PreNode(head, olda, oldb);
    if (pre != NULL && pre->pNext != NULL) 
    {
        pre->pNext->a = newa;
        pre->pNext->b = newb;
    }
}

void clear_List(Node *head)
{
    Node* p1 = head->pNext;
    Node* p2 = NULL;
    while (p1 != NULL)
     {
        p2 = p1;
        p1 = p1->pNext;
        free(p2);
    }
    head->pNext = NULL;
}

int main() {
    Node *head = create_head();
    PreAdd_Node(head, 'B', 2);
    PreAdd_Node(head, 'C', 3);
    PreAdd_Node(head, 'D', 4);
    PreAdd_Node(head, 'E', 5);
    TailAdd_Node(head, 'A', 1);
    modValue_Node(head, 'C', 3, 'Z', 26);
    MidAdd_Node(head,'Z',26,'C', 3);
    print_list(head);
    clear_List(head);
    print_list(head);
    free(head);
    return 0;
}    